<template>
	<view class="open_kai_guan_box" :style="{height: windowHeight + 'px'}">
		<view class="item" v-for="(item, index) in openHardWareList" :key="index">
			<view class="left_box">
				<text class="vertical_text mn fs_36">{{ item.titleMn }}</text>
				<text class="vertical_text mn fs_36">ID : {{ item.id }}</text>
				<text class="vertical_text mn fs_36" v-if="item.swithType == 0">ᠬᠠᠭᠠᠵᠥ ᠪᠠᠢᠨ᠎ᠠ</text>
				<text class="vertical_text mn fs_36" v-if="item.swithType == 1">ᠨᠡᠭᠡᠭᠡᠵᠥ ᠪᠠᠢᠨ᠎ᠠ</text>
				<text class="vertical_text mn fs_36">ᠨᠡᠭᠡᠭᠡᠭᠰᠡᠨ ᠬᠤᠭᠤᠴᠠᠭ᠎ᠠ : {{ item.time }}</text>
			</view>
			
			<view class="right_box">
				<i class="iconfont icon-38kaiguanguan1 color_ccc" v-if="item.swithType == 0" @click="swithKaiGuan(index, 1)"></i>
				<i class="iconfont icon-kaiguan mainColor" v-if="item.swithType == 1" @click="swithKaiGuan(index, 0)"></i>
			</view>
		</view>
	</view>
</template>

<script>
	import getSystemInfo from '@/mixins/basic.js'
	const NET = require('@/utils/request')
	const API = require('@/config/api')
	
	export default {
		mixins: [getSystemInfo],
		data() {
			return {
				openHardWareList: [],
			}
		},
		onLoad() {
			// 获取设备高度
			this.getSystemHeight()
		},
		mounted() {
			this.getOpenHardWareList() 
		},
		methods: {
			// 获取正在运行的设备
			getOpenHardWareList() {
				NET.request(API.getOpenHardWareList, 'GET')
				.then(res => {
					console.log('获取 open 开关: ', res)
					if (res.code == 200) {						
						this.openHardWareList = res.data
					}
				})
				.catch(err => {
					console.error('获取 open 开关失败 : ', err)
				})
			},
			
			// 控制开关
			swithKaiGuan(index, val) {
				let id = this.openHardWareList[index].id
				let swithType = val
				
				NET.request(API.swithHardWare, { id, swithType }, 'GET')
				.then(res => {
					// console.log('控制开关 : ', res)
					if (res.code == 200) {
						this.openHardWareList[index].swithType = val
					}
					
					setTimeout(() => {
						this.getKaiGuanState(id, index, val)
					}, 4000)
			
				})
				.catch(err => {
					console.error('控制开关失败 : ', err)
				})
			},
			
			// 获取开关状态
			getKaiGuanState(id, i, val) {
				NET.request(API.getKaiGuanState, { id }, 'GET')
				.then(res => {
					// console.log('获取开关状态 : ', res)
					if (res.code == 200) {
						if (res.data !== val) {
							uni.showToast({
								title: '开关控制失败！', icon: 'error'
							})
							this.openHardWareList[i].swithType = res.data
						}
					}
				})
				.catch(err => {
					console.error('获取开关状态失败 : ', err)
				})
			}
		}
	}
</script>

<style lang="less" scoped>
	.open_kai_guan_box {
		width: 100%;
		padding: 20rpx;
		box-sizing: border-box;
		margin-bottom: 20rpx;
		
		.item {
			width: 100%;
			border: 1px solid #1c9aa0;
			border-radius: 10rpx;
			padding: 10rpx;
			box-sizing: border-box;
			overflow: hidden;
			
			.left_box {
				width: 50%;
				float: left;
				display: flex;
				flex-direction: row;
				justify-content: space-around;
				// border: 1px solid #1c9aa0;
				
				.mn {
					position: relative;
					top: 0;
				}
			}
			
			.right_box {
				width: 40%;
				float: right;
				// border: 1px solid #1c9aa0;
				
				.iconfont { font-size: 200rpx; }
			}
		}
	}
</style>